package com.zzyl.nursing.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlanDto;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id){
        //1. 查询护理计划的基本信息
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        //2. 查询护理计划id所对应的管理表
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectNursingPlanById( id);
        //3. 将连张表封装到统一数据Vo中
        NursingPlanVo Vo = new NursingPlanVo();
        //将护理计划的基本信息拷贝到Vo中
        BeanUtils.copyProperties(nursingPlan, Vo);
        //因为关联表的id是long类型因此需要修改
        Vo.setId(id.longValue());
        //将关联表数据添加进去
        Vo.setProjectPlans(list);
        return Vo;


    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //保存护理计划
        //属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto){
        try {
            //1.修改护理计划
            NursingPlan nursingPlan = new NursingPlan();
            //将dto属性拷贝到nursingPlan中
            BeanUtils.copyProperties(dto,nursingPlan);
            nursingPlan.setId(dto.getId());
            //2. 判断dto中是否由管理表的数据
            List<NursingProjectPlanDto> list = dto.getProjectPlans();
            if(list != null && list.isEmpty()){
                //3. 代表存在护理项目的列表 , 先删除再添加
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                nursingProjectPlanMapper.batchInsert(list , nursingPlan.getId());
                }
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }




    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id){
        try {
            //1. 删除护理计划
            nursingPlanMapper.deleteNursingPlanById(id.longValue());
            //2. 删除护理计划所对应的关联表数据
           return nursingProjectPlanMapper.deleteNursingProjectPlanById(id.longValue());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteNursingPlanById(id);
    }
}
